package com.salt.modules.sys.web;

import com.salt.common.json.AjaxJson;
import com.salt.core.excel.utils.ExcelUtil;
import com.salt.core.log.annotation.OperationRecord;
import com.salt.core.oss.OssTemplate;
import com.salt.core.oss.model.SaltFile;
import com.salt.core.persistence.Page;
import com.salt.core.tool.utils.StringUtil;
import com.salt.core.web.BaseController;
import com.salt.modules.sys.entity.EditionFiles;
import com.salt.modules.sys.entity.EditionFilesImport;
import com.salt.modules.sys.service.EditionFilesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * (EditionFiles)表控制层
 *
 * @author makejava
 */
@Api(tags = "版本更新信息")
@RestController
@CrossOrigin
@RequestMapping("sys/edition/files")
public class EditionFilesController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private EditionFilesService editionFilesService;
    @Resource
    private OssTemplate ossTemplate;


    /**
     * @param editionFiles 查询内容
     * @param pageNo       页码
     * @param pageSize     每页大小
     * @return
     * @Author yan
     */
    @ApiOperation(value = "分页查询:根据前台参数分页查询")
    @GetMapping("allLimit")
    public AjaxJson list(EditionFiles editionFiles, @RequestParam(required = false) int pageNo, @RequestParam(required = false) int pageSize) {
        Page<EditionFiles> page = editionFilesService.findPage(new Page<EditionFiles>(pageNo, pageSize), editionFiles);
        if (page.getList() != null && page.getList().size() > 0) {
            for (EditionFiles files : page.getList()) {
                if (StringUtil.isNotBlank(files.getNewFileName())) {
                    files.setNewFileName(ossTemplate.outerFileLink(files.getNewFileName()));
                }
            }
        }
        return AjaxJson.success().put("page", page);
    }


    /**
     * @param file
     * @return
     * @Author yan
     */
    @ApiOperation(value = "上传文件的处理 并保持文件信息保存到数据库")
    @PostMapping("/upload")
    public AjaxJson upload(@RequestParam("file") MultipartFile file, EditionFiles editionFiles) {
        if (editionFiles == null || editionFiles.getType() == null) {
            return AjaxJson.success().put("msg", "上传数据不能为空");
        }

        if (editionFiles.getType().equals(0)) {
            if (file.isEmpty()) {
                return AjaxJson.success().put("msg", "文件不能为空");
            }

            //文件类型
            String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
            SaltFile saltFile = ossTemplate.putFile(file);
            //获取文件大小
            long size = file.getSize();
            editionFiles.setSize(String.valueOf(size));
            editionFiles.setExt(extension);
            editionFiles.setOldFileName(saltFile.getOriginalName());
            editionFiles.setNewFileName(saltFile.getName());
            editionFiles.setPath(saltFile.getLink());
            editionFiles.setCreateDate(new Date());
        }
        editionFilesService.saveFile(editionFiles);
        return AjaxJson.success().put("msg", "上传成功").put("id", editionFiles.getId());
    }


    /**
     * 根据主键列表删除数据
     *
     * @param id
     * @return
     * @Author yan
     */
    @ApiOperation(value = "删除文件信息")
    @GetMapping("/delete")
    @OperationRecord(service = "系统服务", module = "版本更新", operation = "删除", description = "")
    public AjaxJson delete(String id) {

        EditionFiles files = editionFilesService.get(id);
        if (files != null) {
            if (files.getType() != 2) {
                ossTemplate.removeFile(files.getNewFileName());
            }
            //删除数据库中的记录
            editionFilesService.deleteById(id);
        }
        return AjaxJson.success().put("msg", "删除成功");
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条数据")
    @GetMapping("selectOne")
    public AjaxJson selectOne(String id) {
        EditionFiles files = this.editionFilesService.get(id);
        return AjaxJson.success().put("fixedFiles", files);
    }

    /**
     * @return
     * @Author yan
     */
    @ApiOperation(value = "上传文件的处理 并保持文件信息保存到数据库")
    @PostMapping("/save")
    public AjaxJson save(EditionFiles editionFiles) {
        editionFilesService.saveFile(editionFiles);
        return AjaxJson.success().put("id", editionFiles.getId());
    }

    @PostMapping("import")
    public AjaxJson importEditionFiles(MultipartFile file) {
        List<EditionFilesImport> bookList =
                ExcelUtil.read(file, 0, 1, EditionFilesImport.class);
        if (bookList != null && !bookList.isEmpty()) {
            editionFilesService.batchSave(bookList);
        }
        return AjaxJson.error("导入失败!");
    }
}